package com.example.education.service.impl;

import com.example.education.exception.ParamException;
import com.example.education.dao.CategoryMapper;
import com.example.education.model.Category;
import com.example.education.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public boolean insertCategory(Category category) throws ParamException {
        if(category==null ){
            throw new ParamException("参数为空");
        }else if(category.getCategoryName()==null ||
                "".equals(category.getCategoryName())){
            throw new ParamException("CategoryName为空");
        }
        Integer result =  categoryMapper.insertCategory(category);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteCategory(Integer categoryId) throws ParamException {
        if(categoryId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  categoryMapper.deleteCategory(categoryId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateCategory(Category category) throws ParamException {
        if(category==null || category.getCategoryName()==null ||
                "".equals(category.getCategoryName())){
            throw new ParamException("参数为空");
        }
        Integer result =  categoryMapper.updateCategory(category);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Category getCategoryById(Integer categoryId) throws ParamException {
        if(categoryId==null){
            throw new ParamException("参数为空");
        }
        return categoryMapper.getCategoryById(categoryId);
    }

    @Override
    public List<Category> selectAll(){
        return categoryMapper.selectAll();
    }
}
